'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
exports.main = async (event, context) => {
	//event为客户端上传的参数
	// console.log('event : ', event)
	let result, {
		keyword,
		flag
	} = event
	let BoxList = [
		"最近上线",
		"近期热门",
		"强力推荐",
		"多人在看"
	]
	let index = BoxList.findIndex(item => item == keyword)
	if (index == -1) {
		result = await db.collection("zhumuta-cash-info")
			.where(dbCmd.and([dbCmd.or({
				author: new RegExp(keyword, 'i')
			}, {
				title: new RegExp(keyword, 'i')
			}, {
				intro: new RegExp(keyword, 'i')
			}), {
				article_status: 1
			}]))
			.get()
	} else {
		switch (index) {
			case 0:
				result = await db.collection('zhumuta-cash-info').where({
					article_status: 1
				}).orderBy("create_time", "desc").get()
				break;
			case 1:
				result = await db.collection('zhumuta-cash-info').where({
					article_status: 1
				}).orderBy("collect_count", "desc").orderBy(
					"view_count", "desc").get()
				break;
			case 2:
				result = await db.collection('zhumuta-cash-info').where({
					article_status: 1
				}).orderBy("collect_count", "desc").get()
				break;
			case 3:
				result = await db.collection('zhumuta-cash-info').where({
					article_status: 1
				}).orderBy("view_count", "desc").get()
				break;
		}
	}
	//返回数据给客户端
	return result
};